import {
  checkLogin
} from './auth.js'
new Vue({
  el: '#app',
  data: {
    IS_LOGIN: null,
    dataList: [],
    dialogFormVisibleEdit: false,
    dialogTreeVisibleEdit: false,
    allPermissions: [],
    defaultProps: {},
    pagination: {
      pageSizes: [5, 10, 20, 50, 100],
      queryString: '',
      currentPage: 1,
      pageSize: 5,
      total: 0
    },
    formData: {
      roleid: '',
      rolename: '',
      keyword: '',
      description: '',
      checkedMenuKeys: []
    }
  },
  async created() {
    this.IS_LOGIN = await checkLogin()
    if (this.IS_LOGIN == null) {
      location.href = '/'
    }
    this.getDataList()
    this.getAllPermissions()
  },
  methods: {
    // 编辑权限角色
    async editRole(row) {
      const roleid = row.roleid
      this.dialogTreeVisibleEdit = true
      await this.getRoleAndMenuById(roleid)
    },
    async getRoleAndMenuById(roleid) {
      const res = await request.get('/role/getRoleById',{
        params:{
          id: roleid
        }
      })
      this.formData.roleid = res.data.roleid
      this.formData.rolename = res.data.rolename
      this.formData.description = res.data.description
      this.formData.checkedMenuKeys = res.data.menuIds
      this.formData.keyword = res.data.keyword
      this.$refs.edittree.setCheckedKeys(res.data.menuIds)
    },
    // 保存修改权限角色
    async setRolePermissions() {
      const res = await request.put('/role/updateRole', {
        roleid: this.formData.roleid,
        rolename: this.formData.rolename,
        description: this.formData.description,
        keyword: this.formData.keyword,
        menuIds: this.formData.checkedMenuKeys
      })
      this.dialogTreeVisibleEdit = false
      this.$message.success(res.msg)
    },
    handleCheckChange(data,obj) {
      this.formData.checkedMenuKeys = obj.checkedKeys
    },
    cancelAddRolePermissions () {
      this.$refs.tree.setCheckedKeys([])
      this.dialogFormVisibleEdit = false
    },
    // 添加权限角色
    async addRolePermissions() {
      if (this.formData.rolename == '') {
        this.$message.error('请输入角色名称')
        return
      }
      if (this.formData.description == '') {
        this.$message.error('请输入角色描述')
        return
      }
      if (this.formData.keyword == '') {
        this.$message.error('请输入角色关键字')
        return
      }
      const res = await request.post('/role/addRole',{
        rolename: this.formData.rolename,
        description: this.formData.description,
        keyword: this.formData.keyword,
        menuIds: this.formData.checkedMenuKeys
      })
      this.getDataList()
      this.$refs.tree.setCheckedKeys([])
      this.dialogFormVisibleEdit = false
      this.$message.success(res.msg)
    },
    // 获取菜单列表
    async getAllPermissions() {
      const res = await request.get('/menu/getAllMenuList')
      this.allPermissions = res.data
      this.defaultProps = {
        children: 'children',
        label: 'title'
      }
    },
    // 获取角色列表
    async getDataList() {
      const res = await request.get('/role/getRolePageList',{
        params: {
          queryString: this.pagination.queryString,
          currentPage: this.pagination.currentPage,
          pageSize: this.pagination.pageSize
        }
      })
      this.dataList = res.data.list
      this.pagination.total = res.data.pageTotal
      console.log(res)
      console.log(this.dataList)
    },
    handleCreate() {
      this.dialogFormVisibleEdit = true
    },
    async lockRole(item) {
      console.log('锁定角色',item);
      const params = new URLSearchParams();
      params.append('id', item.roleid)
      params.append('status', item.status == 1 ? 0 : 1)
      const res = await request.put('/role/lockRole', params)
      this.getDataList()
      this.$message.success(res.msg)
    },
    findPage() {
      this.getDataList()
    },
    handleSizeChange(size) {
      this.pagination.pageSize = size
      this.getDataList()
    },
    handleCurrentChange(currentPage) {
      this.pagination.currentPage = currentPage
      this.getDataList()
    }
  }
})